package com.tool.btn;

import cn.hutool.core.util.StrUtil;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.XYJpanel;
import org.come.Jpanel.XYXJpanel;
import org.come.Jpanel.XYXYDJpanel;
import org.come.Jpanel.XinYuanChengShengJpane;
import org.come.entity.RoleSummoning;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.UserMessUntil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class XYBtn extends MoBanBtn {
    public int caozuo;
    public XYJpanel xyJpanel;
    public XYXYDJpanel xyxydJpanel;
    public static int max = 0;
    public XinYuanChengShengJpane XinYuanChengShengJpane;

    public XYXJpanel xyxJpanel;

    public XYBtn(String iconpath, int type, int cao, XYJpanel xyJpanel) {
        super(iconpath, type );
        this.caozuo=cao;
        this.xyJpanel=xyJpanel;
    }

    public XYBtn(String iconpath, int type, int cao, XYJpanel xyJpanel, int x) {
        super(iconpath, type);
        this.caozuo=cao;
        this.xyJpanel=xyJpanel;
    }

    public XYBtn(String iconpath, int type, int cao, XYXYDJpanel xyxydJpanel) {
        super(iconpath, type);
        this.caozuo=cao;
        this.xyxydJpanel=xyxydJpanel;
    }


    public XYBtn(String iconpath, int type, int cao, XYXYDJpanel xyxydJpanel, int x) {
        super(iconpath, type);
        this.caozuo=cao;
        this.xyxydJpanel=xyxydJpanel;
    }




    public XYBtn(String iconpath, int type, int cao, XYJpanel xyJpanel, String text) {
        super(iconpath, type);
        this.caozuo=cao;
        this.xyJpanel=xyJpanel;
        this.setText(text);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
    }
    public XYBtn(String iconpath, int type, int cao, XYXYDJpanel xyxydJpanel, String text) {
        super(iconpath, type);
        this.caozuo=cao;
        this.xyxydJpanel=xyxydJpanel;
        this.setText(text);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
    }
    public XYBtn(String iconpath, int type, int cao, XinYuanChengShengJpane XinYuanChengShengJpane, String text) {
        super(iconpath, type);
        this.caozuo=cao;
        this.XinYuanChengShengJpane=XinYuanChengShengJpane;
        this.setText(text);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
    }

    public XYBtn(String iconpath, int type, int cao, XYXJpanel xyxJpanel, int text) {
        super(iconpath, type);
        this.caozuo=cao;
        this.xyxJpanel=xyxJpanel;
        this.setText(null);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
    }

    @Override
    public void chooseyes() {

    }

    @Override
    public void chooseno() {

    }

    @Override
    public void nochoose(MouseEvent e) {
        RoleSummoning pet = UserMessUntil.getChosePetMes();
        if (caozuo == 17) return;
        if (caozuo == 18) {
            FormsManagement.showForm(128);
            FormsManagement.HideForm(124);
            FormsManagement.HideForm(125);

            for (XYBtn xyBtn : XYXJpanel.xinlu) {
                xyBtn.btnchange(0);
            }
            return;
        }
        if (caozuo == 16) {
            FormsManagement.HideForm(128);
            FormsManagement.HideForm(124);
            FormsManagement.showForm(125);

            for (XYBtn xyBtn : XYJpanel.xinlu) {
                xyBtn.btnchange(0);
            }


            return;
        }
        if (caozuo == 45) {
            FormsManagement.HideForm(125);
            FormsManagement.HideForm(128);
            SendMessageUntil.toServer(Agreement.getAgreement().rolechangeAgreement("OPEN1" + "&" + pet.getSid()));

            for (XYBtn xyBtn : XYXYDJpanel.xinlu) {
                xyBtn.btnchange(0);
            }


            return;
        }
        if (caozuo < 30) {
            try {
                if (Integer.parseInt(xyJpanel.getNum()) > 0) {
                    if (xyJpanel.getXiulian()[caozuo].getText().equals("")) {
                        switch (caozuo) {
                            case 0:
                            case 1:
                            case 2:
                        }

                        int x = 0;
                        for (int i = 0; i < xyJpanel.getXiulian().length; i++) {
                            String text = xyJpanel.getXiulian()[i].getText();
                            if(text.equals("true")){
                                x++;
                            }
                        }

                        Integer integer = XYJpanel.pointRule.get(caozuo);
                        if(x < integer){
                            ZhuFrame.getZhuJpanel().addPrompt2("#R心意点数不满足条件");
                            return;
                        }

                        //3 4 5 6 7 , 11 12 13 14 15
                        String jidian = xyJpanel.getJidian();
                        if(StrUtil.isNotBlank(jidian)){
//                            //system.out.println(jidian);

                            List<Integer> numbersList = Arrays.stream(jidian.split("#"))
                                    .filter(s -> !s.isEmpty())
                                    .map(Integer::parseInt)
                                    .distinct()
                                    .collect(Collectors.toList());

                            if(caozuo > 3 && caozuo < 8){
                                if (!numbersList.contains(caozuo-1)) {
                                    ZhuFrame.getZhuJpanel().addPrompt2("#R前置条件不满足");
                                    return;
                                }
                            }

                            if(caozuo > 11 && caozuo <= 15){
                                if (!numbersList.contains(caozuo-1)) {
                                    ZhuFrame.getZhuJpanel().addPrompt2("#R前置条件不满足");
                                    return;
                                }
                            }

                        }

                        xyJpanel.getXiulian()[caozuo].setIcons(CutButtonImage.cuts("inkImg/old/background/S413.png"));
                        xyJpanel.getXiulian()[caozuo].setText("true");
                        xyJpanel.setNum(String.valueOf(Integer.parseInt(xyJpanel.getNum()) - 1));
                        if (xyJpanel.getJidian() == null)
                            xyJpanel.setJidian("#" + caozuo);
                        else xyJpanel.setJidian(xyJpanel.getJidian() + "#" + caozuo);

                    } else ZhuFrame.getZhuJpanel().addPrompt2("#不能重复点击");

                  /*  String mes=null;
                    for (int k=0;k<=xyJpanel.getXiulian().length-1;k++){

                    }
                    SendMessageUntil.toServer(Agreement.getAgreement().rolechangeAgreement("Y#"+caozuo+"#"+xyJpanel.getNum()));*/
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("#R当前心意点不足");

                }

            } catch (Exception exception) {
                exception.printStackTrace();
            }
        } else if (caozuo <= 42) {
            if (caozuo != 30) {
                if (xyxydJpanel.panduan[caozuo - 31] == 0) {
                    ZhuFrame.getZhuJpanel().addPrompt2(caozuo - 30 + "号心路未完成！无法点亮新心路");
                    return;
                }

            }
            int lvl = Integer.parseInt(changeGrade(pet.getGrade()).split("转")[1]);
            int grad = Integer.parseInt(changeGrade(pet.getGrade()).split("转")[0]);
            int num = 0;//高级技能个数
            int zhongji = 0;//终级技能
            int gezi = pet.getOpenSeal();
            int longgu = pet.getDragon();
            if (pet.getPetSkills()!=null){
                String[] skill = pet.getPetSkills().split("\\|");
                for (String m : skill) {
                    int skinInt = Integer.parseInt(m);
                    if (skinInt==1886||
                            skinInt==1887||
                            skinInt==1600||
                            skinInt==1601||
                            skinInt==1602||
                            skinInt==1603||
                            skinInt==1604||
                            skinInt==1605||
                            skinInt==1611||
                            skinInt==1612||
                            skinInt==1811||
                            skinInt==1815||
                            skinInt==1816||
                            skinInt==1817||
                            skinInt==1818||
                            skinInt==1819||
                            skinInt==1820||
                            skinInt==1821||
                            skinInt==1822||
                            skinInt==1823||
                            skinInt==1824||
                            skinInt==1825||
                            skinInt==1826||
                            skinInt==1827||
                            skinInt==1831||
                            skinInt==1833||
                            skinInt==1834||
                            skinInt==1835||
                            skinInt==1836||
                            skinInt==1837||
                            skinInt==1838||
                            skinInt==1839||
                            skinInt==1850||
                            skinInt==1852||
                            skinInt==1854||
                            skinInt==1858||
                            skinInt==1859||
                            skinInt==1860||
                            skinInt==1862||
                            skinInt==1864||
                            skinInt==1865||
                            skinInt==1871||
                            skinInt==1872||
                            skinInt==1873||
                            skinInt==1874||
                            skinInt==1875||
                            skinInt==1876||
                            skinInt==1877||
                            skinInt==1878||
                            skinInt==1879||
                            skinInt==1880||
                            skinInt==1882||
                            skinInt==1883||
                            skinInt==1884||
                            skinInt==1885)
                        num = num + 1;
                }
                for (String m : skill) {
                    int skillInt = Integer.parseInt(m);
                    if (skillInt==1606||
                            skillInt==1607||
                            skillInt==1608||
                            skillInt==1828||
                            skillInt==1829||
                            skillInt==1830||
                            skillInt==1840||
                            skillInt==1841||
                            skillInt==1842||
                            skillInt==1867||
                            skillInt==1868||
                            skillInt==1869
                    )//判断ID啊 技能的ID
                        zhongji = zhongji + 1;
                }
            }

            switch (caozuo - 30) {

                case 0:
                    if (lvl < 50) {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 1:
                    if (pet.getFriendliness() < 100000) {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 2:
                    if (lvl < 120 || pet.getFriendliness() < 200000) {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 3:
                    if (lvl < 100 || grad < 2 || pet.getFriendliness() < 300000)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 4:
                    if (lvl < 120 || grad < 2 || pet.getFriendliness() < 400000)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;

                case 5:
                    if (lvl < 140 || gezi < 2)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 6:
                    if (pet.getFriendliness() < 500000)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 7:
                    if (num < 2 || grad < 3 || lvl < 140)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 8:
                    if (pet.getFriendliness() < 1000000)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 9:
                    if (num < 3 || pet.getFriendliness() < 2000000 || lvl < 160)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 10:
                    if (gezi < 4 || pet.getFriendliness() < 4000000 || lvl < 170 || longgu < 1)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 11:
                    if (gezi < 6 || pet.getFriendliness() < 6000000 || lvl < 175 || longgu < 3)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
                case 12:
                    if (num < 4 || pet.getFriendliness() < 10000000 || lvl < 180 || zhongji < 2)//等级和亲密
                    {
                        ZhuFrame.getZhuJpanel().addPrompt2("条件未满足！无法点亮新心路");
                        return;
                    }
                    break;
            }
            try {
                xyxydJpanel.getXiulian()[caozuo - 30].setIcons(CutButtonImage.cuts("skin/8100226/mouse/xy_xl_btn_5.png"));
            } catch (Exception exception) {
                exception.printStackTrace();
            }


            max = caozuo - 30;
            xyxydJpanel.panduan[caozuo - 30] = 1;
            //xyJpanel.setNum(String.valueOf(Integer.parseInt(xyJpanel.getNum())-1));

        }
        if (caozuo == 43) {
            int k = 0;
            for (int i = 0; i <= xyxydJpanel.getPanduan().length - 1; i++) {
                if (xyxydJpanel.getPanduan()[i] == 1) {
                    k = k + 1;
                }
            }
            if (xyJpanel.getJidian() == null)
                SendMessageUntil.toServer(Agreement.getAgreement().rolechangeAgreement("Y#" + k + "&" + pet.getSid()));
            else {
                int l = 0;
                for (int n = 0; n <= xyJpanel.getXiulian().length - 1; n++) {
                    if (xyJpanel.getXiulian()[n] != null && xyJpanel.getXiulian()[n].getText().equals("true")) {
                        l = l + 1;
                    }
                }
                k = k - l;
//                pet.setXy("Y#" + xyJpanel.getJidian() + "#" + xyJpanel.getNum());
                SendMessageUntil.toServer(Agreement.getAgreement().rolechangeAgreement("Y#" + xyJpanel.getJidian() + "#" + k + "&" + pet.getSid()));
            }

            double x = xyxydJpanel.getXiulian()[max].getBounds().getX();
            double y = xyxydJpanel.getXiulian()[max].getBounds().getY();
            XYXYDJpanel.xyxtX = (int)x;
            XYXYDJpanel.xyxtY = (int)y;

        }
        if (caozuo == 44) {
            try {
                if(xyJpanel!=null){
                    String[] split = xyJpanel.getJidian().split("&")[0].split("#");
                    for (int i = 1; i < split.length; i++) {
                        xyJpanel.getXiulian()[Integer.parseInt(split[i])].setIcons(CutButtonImage.cuts("inkImg/old/background/S412.png"));
                        xyJpanel.getXiulian()[Integer.parseInt(split[i])].setText("true");
                    }
                    pet.setXy("Y#" + xyJpanel.getJidian() + "#" + xyJpanel.getNum());
                    SendMessageUntil.toServer(Agreement.getAgreement().rolechangeAgreement("Y#" + xyJpanel.getJidian() + "#" + xyJpanel.getNum() + "&" + pet.getSid()));
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
        if (caozuo == 46) {
            XYXYDJpanel.xyxtX = 0;
            XYXYDJpanel.xyxtY = 0;
            //      if (){
            pet.setXy(null);
            SendMessageUntil.toServer(Agreement.getAgreement().rolechangeAgreement("Y#重置" + "&" + pet.getSid()));
//            }else {
//                ZhuFrame.getZhuJpanel().addPrompt("没有点数可重置");
//                return;
//            }

        }
        if (caozuo==444){
            FormsManagement.showForm(126);
            FormsManagement.HideForm(124);
            FormsManagement.HideForm(125);
            FormsManagement.HideForm(18);
            FormsManagement.HideForm(6);
        }
        if (caozuo==445){
            if (pet != null && !pet.getSummoningid().equals("200221")) {
                ZhuFrame.getZhuJpanel().addPrompt2("当前参战召唤兽不是心猿，想成圣？做梦去吧！");
                return;
            }
            SendMessageUntil.toServer(Agreement.getAgreement().rolechangeAgreement("Y#成圣" + "&" + pet.getSid()));
            FormsManagement.HideForm(126);
        }
        if (caozuo==446){
            FormsManagement.HideForm(126);
        }
    }
    public static String changeGrade(int grade) {

        // 判断召唤兽的等级
        String mes=null;
        if (grade <= 100) {



            mes= "0转" + grade ;

        } else if ( grade <= 221) {




            mes= "1转" + (grade - 101) ;
        } else if ( grade <= 362) {



            mes= "2转" + (grade - 222) ;

        } else if ( grade <= 543) {



            mes= "3转" + (grade - 363) ;

        } else {


            mes= "4转" + (grade - 544) ;
        }
        return  mes;
    }
}
